//app.js
var common = require("utils/common.js");

App({
  onLaunch: function () {
    //调用API从本地缓存中获取数据
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
  },
  getUserInfo:function(cb){
    var that = this
    if(this.globalData.userInfo){
      // console.log('globalData')
      typeof cb == "function" && cb(this.globalData.userInfo)
    }else{
      // console.log('loginData')
      //调用登录接口
      wx.login({
        success: function (resLogin) {
          wx.getUserInfo({
            success: function (resUserInfo) {
              that.globalData.userInfo = resUserInfo.userInfo
              typeof cb == "function" && cb(that.globalData.userInfo)
            }
          })
        }
      })
    }
  },
  getToken:function(cb){
    var that = this
    // if(this.globalData.wangcaiToken){
    //   console.log('globalDataToken')
    //   // typeof cb == "function" && cb(this.globalData.wangcaiToken)
    // }else{
      // console.log('loginDataToken')
      //调用登录接口
      wx.login({
        success: function (resLogin) {
          console.log('调用登录接口wx.login成功')
          wx.getUserInfo({
            success: function (resUserInfo) {
              // console.log('获取用户信息接口wx.getUserInfo成功')
              that.globalData.userInfo = resUserInfo.userInfo
              // typeof cb == "function" && cb(that.globalData.userInfo)
                if(resLogin.code){
                  wx.request({
                    url: common.apiUrl()+'jscode2session',
                    method:'POST',
                    header: {
                    'content-type': 'application/x-www-form-urlencoded'
                      },
                    data: {
                      code:resLogin.code,
                      nickname:resUserInfo.userInfo.nickName,
                      avatarurl:resUserInfo.userInfo.avatarUrl,
                      gender:resUserInfo.userInfo.gender,
                      province:resUserInfo.userInfo.province,
                      city:resUserInfo.userInfo.city,
                      country:resUserInfo.userInfo.country
                    },
                    success: function(resCode2Session){
                      // success
                      that.globalData.wangcaiToken = resCode2Session.data.data
                      wx.setStorageSync("wangcai_token",resCode2Session.data.data)
                      typeof cb == "function" && cb(that.globalData.wangcaiToken)
                      // 为获取用户唯一的unionid,来区分用户的唯一性
                      // wx.request({
                      //   url: common.apiUrl()+'decodeUserInfo',
                      //   data: {
                      //     encryptedData:resUserInfo.encryptedData,
                      //     iv:resUserInfo.iv,
                      //     sessionKey:resCode2Session.data.data.session_key
                      //   },
                      //   success: function(resDecodeUserInfo){
                      //     // success
                      //     console.log(resDecodeUserInfo.data)
                      //   }
                      // })
                    }
                  })
                }
            },
            fail:function(res){
              wx.showModal({
                title: res.errMsg,
                content: '授权失败，请删除小程序后重新接受授权',
                showCancel:false
              })
              typeof cb == "function" && cb(that.globalData.wangcaiToken)
            }
          })
        }
      })
  // }
  },
  getRecordParam:function(cb){
    var that = this
    wx.showToast({
      title: '加载中',
      icon: 'loading'
    })
    // console.log('请求getRecordParam')
    wx.request({
        url: common.apiUrl()+'recordParam',
        data: {
          token:that.globalData.wangcaiToken
        },
        success: function(res){
          wx.hideToast()
          if(res.data.status==1){
            that.globalData.recordParam = res.data.data;
          }
          typeof cb == "function" && cb(res.data)
        }
      })
  },
  errorToGetToken:function(){
    var that = this
    wx.showModal({
      title: '错误提示',
      content: '数据获取失败，请重新载入',
      showCancel:false,
      success: function(res) {
        if (res.confirm) {
          that.getToken(function(wangcaiToken){
              //刷新上一页数据
              var pages = getCurrentPages();
              if(pages.length > 1){
                  //上一个页面实例对象
                  var prePage = pages[1];
                  prePage.getData()
              }
              wx.navigateBack({
              delta: pages.length - 2
              })
          })
        }
      }
    })
  },
  globalData:{
    userInfo:null,
    wangcaiToken:null,
    recordParam:null,
    // typeList轮播图列表
    typeList:null,
    // addrecord的当前轮播图页
    swiperIndex:null,
    // 之前的选中项:[1,0]代表第1页的第0项
    oldTypeIndex:null,
    // 成员的序号
    memberIndex:null,
    // 方式的序号
    paternIndex:null
  }
})